﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using IGolovkine.Core.Diagnostics;
using System.IO;
using System.ServiceModel;

namespace IGolovkine.ShoutCastRipper.Service.Windows
{
    public partial class RadioRipperWindowsService : ServiceBase
    {
        private StationRipperService _service;
        private AdminService _adminService;
        private ServiceHost _serviceHost;
        public RadioRipperWindowsService()
        {
            try
            {
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
                InitializeComponent();
                var config = ServiceConfigurationManager.LoadServiceProperties();
                _service = new StationRipperService(config);
                _adminService = new AdminService(_service);
                _serviceHost = new ServiceHost(_adminService);

            }
            catch (Exception ex)
            {
                DumpException(ex);
                WriteToEventLog("{0}: {1}", ex.Message, ex.StackTrace);
                
                throw ex;
            }
        }

        private void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            DumpException(ex);
            WriteToEventLog("Unhandled exception {0}: {1}", ex.Message, ex.StackTrace);
        }

        private static void WriteToEventLog(string format, params object[] args)
        {
            string EventSource = typeof(RadioRipperWindowsService).Name;
            string message = string.Format(format, args);
            EventLog.WriteEntry(EventSource, message, EventLogEntryType.Error);
        }

        private static void DumpException(Exception ex)
        {
            StringBuilder buffer = new StringBuilder();
            buffer.AppendLine(ObjectDumper.Format(ex));
            string fileName = string.Format("CurrentDomain_UnhandledException_{0:yyyy_MMM_dd_HH_mm_ss}.txt", DateTime.Now);
            File.WriteAllText(fileName, buffer.ToString());
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                _service.Start();
                _serviceHost.Open();
            }
            catch (Exception ex)
            {
                DumpException(ex);
                WriteToEventLog("{0}: {1}", ex.Message, ex.StackTrace);
                this.Stop();
                throw ex;
            }
        }

        protected override void OnStop()
        {
            try
            {
                _service.Stop();
            }
            catch (Exception ex)
            {
                DumpException(ex);
                WriteToEventLog("{0}: {1}", ex.Message, ex.StackTrace);
                throw ex;
            }
        }
    }
}
